<template>
  <div>
    <!-- 面包屑导航 S -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 面包屑导航 E -->
    <!-- 主体卡片区域 S -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model.trim="queryData.query" clearable
            @clear="getOrderList" @blur="getOrderList">
            <el-button slot="append" class="el-icon-search" @click="getOrderList"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <template>
        <!-- 用户列表 S -->
        <el-table :data="orderList" border style="width: 100%" stripe>
          <!-- 自增序号列 -->
          <el-table-column type="index" label="#">
          </el-table-column>
          <!-- 订单编号列 -->
          <el-table-column prop="order_number" label="订单编号">
          </el-table-column>
          <!-- 订单价格列 -->
          <el-table-column prop="order_price" label="订单价格">
          </el-table-column>
          <!-- 是否付款列 -->
          <el-table-column label="是否付款">
            <template #default="{row}">
              <el-tag :type="row.pay_status ? 'success' :  'danger'">
                {{row.pay_status ? '已付款' :  '未付款'}}
              </el-tag>
            </template>
          </el-table-column>
          <!-- 是否发货列 -->
          <el-table-column prop="is_send" label="是否发货">
          </el-table-column>
          <!-- 下单时间列 -->
          <el-table-column label="下单时间">
            <template #default="{row}">
              {{row.create_time * 1000 | formDate('yyyy-MM-dd')}}
            </template>
          </el-table-column>
          <!-- 操作按钮列  S -->
          <el-table-column label="操作" width="240">
            <template #default="{row}">
              <!-- 修改地址按钮 -->
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="editOrderAddr()">
                修改地址
              </el-button>
              <!-- 物流进度按钮 -->
              <el-button type="success" size="mini" icon="el-icon-truck"
                @click="orderTracking(row.order_id)">
                物流进度
              </el-button>
            </template>
          </el-table-column>
          <!-- 操作按钮列  E -->
        </el-table>
        <!-- 分页器 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryData.pagenum" :page-sizes="[5, 10, 15, 20]"
          :page-size="queryData.pagesize" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </template>
    </el-card>
    <!-- 主体卡片区域 E -->
    <!-- 修改地址的提示框 S -->
    <el-dialog title="修改地址" :visible.sync="editAddrVisible" width="50%" @close="resetEditAddrForm">
      <el-form ref="editAddrForm" :model="editAddrForm" label-width="80px"
        :rules="editAddrFormRules">
        <el-form-item label="省市区县" prop="orderCity">
          <el-cascader :options="cityOptions" v-model="editAddrForm.orderCity">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="orderAddr">
          <el-input v-model="editAddrForm.orderAddr"></el-input>
        </el-form-item>
      </el-form>
      <!-- 提示框按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editAddrVisible = false">取 消</el-button>
        <el-button type="primary" @click="editAddrVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改地址的提示框 E -->
    <!-- 物流进度的提示框 S -->
    <el-dialog title="物流进度" :visible.sync="trackingVisible" width="50%">
      <el-timeline reverse>
        <el-timeline-item v-for="(activity, index) in activities" :key="index" :icon="activity.icon"
          :type="activity.type" size="large" :timestamp="activity.time">
          {{activity.content}}
        </el-timeline-item>
      </el-timeline>
      <!-- 提示框按钮 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="trackingVisible = false">取 消</el-button>
        <el-button type="primary" @click="trackingVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流进度的提示框 E -->
  </div>
</template>

<script>
import cityOptions from './citydata.js'
const checkCity = (r, value, callback) => {
  if (value.length < 2) return callback(new Error())
  return callback()
}
export default {
  data() {
    return {
      // 查询订单列表的参数
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单的总条数
      total: 0,
      // 订单列表
      orderList: [],
      // 修改地址对话框的开关
      editAddrVisible: false,
      // 修改地址的表单数据
      editAddrForm: {
        orderCity: [],
        orderAddr: ''
      },
      // 修改地址表单的校验规则
      editAddrFormRules: {
        orderCity: [
          { required: true, message: '请选择订单地址', trigger: 'change' },
          { validator: checkCity, message: '地址必须具体到区县！', trigger: 'change' }
        ],
        orderAddr: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
          { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
        ]
      },
      // 省市区县的数据列表
      cityOptions: cityOptions,
      // 物流进度对话框的开关
      trackingVisible: false,
      // 物流进度的数据列表
      activities: []
    }
  },

  created() {
    // 调用获取订单列表函数
    this.getOrderList()
  },

  methods: {
    // 获取订单列表函数
    async getOrderList() {
      const data = await this.$axios.get('/orders', { params: this.queryData })
      if (data.meta.status !== 200) return this.$message.error('获取权限列表失败!')
      this.orderList = data.data.goods
      this.total = data.data.total
    },
    // 改变每页显示条数时触发
    handleSizeChange(pagesize) {
      this.queryData.pagesize = pagesize
      this.getOrderList()
    },
    // 改变页码时触发
    handleCurrentChange(pagenum) {
      this.queryData.pagenum = pagenum
      this.getOrderList()
    },
    // 点击修改地址按钮后触发
    editOrderAddr() {
      this.editAddrVisible = true
    },
    resetEditAddrForm() {
      this.$refs.editAddrForm.resetFields()
    },
    // 点击物流进度按钮后触发
    async orderTracking(id) {
      this.trackingVisible = true
      const data = await this.$axios.get('http://localhost:8080/api/express')
      if (data.meta.status !== 200) return this.$message.error('获取物流信息失败！')
      this.activities = data.data
      this.activities[this.activities.length - 1].icon = 'el-icon-location-outline'
      this.activities[this.activities.length - 1].type = 'success'
    }
  }
}
</script>

<style scoped lang='less'>
.el-cascader {
  width: 100%;
}
.el-timeline {
  margin-left: 20px;
  /deep/ .el-icon-location-outline {
    font-size: 12px;
  }
}
</style>
